Welcome back. So, we've already covered booleans which allow us
to write expressions that evaluate to either true or false.
But knowing if something is true or false isn't typically that useful on it's own.
Most of the time when we use Booleans,
we use them in combination with conditional execution.
Conditional execution allows us to say,
"Run this piece of code if something is true,
run this piece of code if something else is true,
or run this piece of code if something is false".
So, let's jump right into conditional execution.
So, we do conditional execution with if and else statements.
So, in this code,
we declare a variable X and set it here to the value 15.
Then, we say, if x modulo two equals equals zero.
As a quick reminder,
modulo is the remainder operator.
So, for example, if we take 15 modulo two,
then because 15 divided by two is seven with a remainder of one,
modulo gives us only are there remainder.
So, 15 modulo two is one.
If we did 14 modulo two,
then because 14 divides evenly into two,
a seven then we get a remainder of zero.
If we did 13 modular two,
then we get a remainder of one and so on.
So, in other words, if we take x modulo two,
then it's going to be zero if
x is even and it can be divided by two with no remainder,
and it's one if x is odd.
So, in this code,
we say if x modulo two is zero,
which to us this is a Boolean expression that's going to evaluate to
true if x is even and it's going to evaluate to false if x is odd.
So, we say if it's even,
then print out x is even then we say else,
so you can think of else as being like otherwise.
So else is saying otherwise if this condition is false,
then do something else.
So, we say if x is even,
print out x is even.
Otherwise, if x is not even,
print out x is odd.
So when I run this code while x is 15,
then we should expect this else condition to print out x is odd. So, let's run our code.
Okay. So, we printed out 15 is
odd and that indicates that we ran what's in this else clause.
Let's modify our code slightly to set x instead to
an even number like 14 and run it again.
Now, we instead get 14 is even.
So, one thing to note is that we specify what's inside of this if through indentation,
so just the same way that we specify
what's inside of a for-loop versus outside of the for-loop.
Just like for-loops, we can have any number of lines here.
So, I can say, "print it is even" in all capital letters,
and this is inside of this if.
I can put here print it is odd.
So, now, when I run my code,
I get 14 is even,
it is even and this else clause is ignored.
So, let's answer some questions around conditional execution.
So, this question asks,
how many lines of code can appear in
the indented code block below the if and else lines in a conditional?
So, in other words, what this is asking,
is after we have an if,
then we have some Boolean expression.
How many lines can we have inside of here?
Well really, we can have pretty much any number of lines.
So, I'm going to put one or more.
Because we have to have at least one thing inside of this block but we can have more,
we can have any number of things inside of
our if block and same thing with our else block.
So, this question asks,
what does the following code print?
Choose from A, B, C or nothing.
So, here, we say if and then we have
a Boolean expression four plus five equals equals ten.
So, when Python evaluates this Boolean expression,
it's first going to evaluate four plus five
because that's higher precedence then equals equals,
and so four plus five evaluates to nine and then it says,
"Is nine equal to 10?"
When it evaluates this expression,
then we get false.
So, what that means is if we say if and then this expression evaluates to false,
then we do not run what's inside of this if clause,
instead we run what's inside of the else clause.
Inside of the else clause,
we have print false and so that's what I expect to be printed in this code.
So, I'm going to answer B.
Okay. Here's a slightly trickier question.
We have the same conditional that we had in the previous question.
So, from that, I'm just going to note that this is false.
So, this if does not run but what's inside of this else runs.
Now, if we look after this else,
then you should see this print true is not indented.
So, in other words, this print true is outside of this if else clause.
So, what's inside of this else clause is just print false.
What that means is that this print true is
run regardless of what happens in this if-else clause.
So, from the previous question,
we know that the else clause is run here because nine is not equal to 10.
So, we know that this print false is what runs first,
but then this print true gets
executed regardless of what happened in this if-else clause.
So, I expect false to be printed,
then true to be printed,
so in other words answer is C. So,
now I'm just going to go through one of these questions.
The rest are fairly similar to this,
so you should try them on your own.
Here, we're asked to write code that assigns a string you can apply to SI to output.
If the string SI 106 is in the list courses.
So, I'm going to start by writing
the Boolean expression to evaluate whether this is true or false.
So, is the string SI 106 inside of the list courses.
So, you might remember that the way that we do that is we can say,
"SI 106 in courses."
This is a Boolean expression that evaluates to true if SI
106 is in courses and false if SI 106 is not in courses.
Now, if I want to do something,
if this Boolean is true,
then I can write If and then add a colon after this.
Now, what do I want to do if SI 106 is in courses.
Well, it says write code to assign the string you can
apply to SI to the variable output in that case.
So, inside of this If, I'm going to write,
"Output equals the string you can apply to SI!".
Now, it says something to do if SI 106 is not in courses.
So, if it's none courses, assign the value,
take SI 106 to the variable output.
So, I'm going to say, "Else".
So, in other words, if SI 106 is not in courses,
output equals take SI 106!.
Okay. So, even though my code doesn't actually print anything here,
you can see from the tests passing that we correctly assigned the variable output.